/**
 *<p>Copyright: Copyright (c) 2012</p> 
 *<p>Company: SCPII</p>
 *<p>Project:scpii-mobi-page</p>
 *@author xiezhouyan
 *@Date 2013-4-27 下午2:07:39
 *@version 1.0
 *@description:
 */
package com.scpii.mobi.page.action.bampo;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.type.TypeReference;

import com.opensymphony.xwork2.Action;
import com.scpii.mobi.page.common.base.BaseAction;
import com.scpii.mobi.page.domain.user.UserLink;
import com.scpii.mobi.page.service.user.UserLinkService;
import com.scpii.mobi.page.util.NetUtils;
import com.scpii.mobi.page.util.XmlUtils;

public class IntegralAction extends BaseAction {

	private static final String VIP_URL = "http://183.60.177.198:8080/Vip.aspx/VipLogin";

	private Map<String, String> result;

	private String username;

	private String password;

	@Resource
	private UserLinkService userLinkService;

	/**
	 * 
	 */
	private static final long serialVersionUID = 4132777303255693691L;

	public String login() {
		return LOGIN;
	}

	@Override
	public String execute() {
		if (user == null) {
			return LOGIN;
		}
		Map<String, String> params = new HashMap<String, String>();
		UserLink record = new UserLink();
		record.setProvider("CARD");
		record.setUserId(user.getId());
		UserLink userLink = userLinkService.selectBySelective(record);
		if (userLink == null) {
			params.put("code", user.getPhoneNumber());
			params.put("pwd", user.getPhoneNumber());
		} else {
			params.put("code", userLink.getCardId());
			params.put("pwd", userLink.getCardPassword());
		}
		try {
			String response = NetUtils.sendPost(VIP_URL, "application/json",
					params);
			Map<String, String> resultMap = NetUtils.MAPPER.readValue(response,
					new TypeReference<Map<String, String>>() {
					});
			String code = StringUtils.split(resultMap.get("d"), "|")[0];
			String xml = StringUtils.split(resultMap.get("d"), "|")[1];
			if ("400".equals(code)) {
				return LOGIN;
			}
			if ("200".equals(code)) {
				result = XmlUtils.dom2Map(xml);
			}
		} catch (Exception e) {
			getRequest().setAttribute("msg", "服务器错误请稍后再试");
			e.printStackTrace();
			return "login";
		}
		return Action.SUCCESS;
	}

	public String sub() {
		Map<String, String> params = new HashMap<String, String>();
		params.put("code", username);
		params.put("pwd", password);
		try {
			String response = NetUtils.sendPost(VIP_URL, "application/json",
					params);
			Map<String, String> resultMap = NetUtils.MAPPER.readValue(response,
					new TypeReference<Map<String, String>>() {
					});
			String code = StringUtils.split(resultMap.get("d"), "|")[0];
			String xml = StringUtils.split(resultMap.get("d"), "|")[1];
			if ("400".equals(code)) {
				getRequest().setAttribute("msg", "用户名或密码错误");
				return LOGIN;
			}
			if ("200".equals(code)) {
				result = XmlUtils.dom2Map(xml);
			}
		} catch (Exception e) {
			getRequest().setAttribute("msg", "服务器错误请稍后再试");
			e.printStackTrace();
			return LOGIN;
		}

		if (user == null) {
			return Action.SUCCESS;
		}
		UserLink record = new UserLink();
		record.setProvider("CARD");
		record.setUserId(user.getId());
		userLinkService.insertSelective(record, username, password);
		return Action.SUCCESS;
	}

	public Map<String, String> getResult() {
		return result;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
